﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Agents.Admin
{
    public partial class RankPage : System.Web.UI.Page
    {
        //page for giving the database percentages for main algorithm
        //explanation: the algorithm gives each client a grade- then, based on that grade
        //decides where the agent should go next. Here the admnistrator decides how much 
        //each kriteria should affect the algorithm 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Set the existing parameters
                Queries queries = new Queries();
                Configuration config = queries.GetConfiguration();

                averageMoneyTxtbox.Text = config.AvgMoney.ToString();
                distanceTxtbox.Text = config.Distance.ToString();
                lastOrderTimeTxtBox.Text = config.LastOrder.ToString();
                priceTxtbox.Text = config.TotalOrders.ToString();
                priorityTxtbox.Text = config.Priority.ToString();
                chkAllowLocationBasedNotifications.Checked = config.AllowLocationBasedNotifications;
                txtRadius.Text = config.LocationBasedNotificationsRadius.ToString();
                mailTxtbox.Text = queries.GetEmail();
            }
        }

        public bool Check(string first, string second, string third, string fourth, string fifth)
        {
            double Num;
            if (fifth == "")
                fifth = "0";
            bool isNum = (double.TryParse(first, out Num)) && (double.TryParse(second, out Num))
                && (double.TryParse(third, out Num)) && (double.TryParse(fourth, out Num))
                && (double.TryParse(fifth, out Num));

            return isNum;
        }

        //function converts from string to double.
        public double change(String s)
        {
            String st = s.Substring(0);
            double x = double.Parse(st);
            return x;
        }

        //function redirects the administrater back to his homepage.
        protected void backBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/AdminMenu.aspx");
        }

        //function is activated when the update button is clicked.
        protected void Update_Click(object sender, EventArgs e)
        {
            //variable for holding the percentages of each kriterea for the algorithm:
            //dist= distance between client and agent
            //total= total amount of orders the client has made.
            //last= how long has it been since his last order.
            //avg= avrage amount of money the client spends per order
            //pri= priority of client (given by administrater)
            double dist, total, last, avg, pri;
            Queries queries = new Queries();

            //making sure the input was numbers
            if (Check(distanceTxtbox.Text, priceTxtbox.Text, lastOrderTimeTxtBox.Text,
                    averageMoneyTxtbox.Text, priorityTxtbox.Text))
            {
                //converting all inputs from strings to double
                String temp = distanceTxtbox.Text;
                if (temp != "")
                    dist = change(temp);
                else dist = 0;

                temp = priceTxtbox.Text;
                if (temp != "")
                    total = change(temp);
                else total = 0;

                temp = lastOrderTimeTxtBox.Text;
                if (temp != "")
                    last = change(temp);
                else last = 0;

                temp = averageMoneyTxtbox.Text;
                if (temp != "")
                    avg = change(temp);
                else avg = 0;

                temp = priorityTxtbox.Text;
                if (temp != "")
                    pri = change(temp);
                else pri = 0;

                //if total does nt add up to 100%
                if (dist + total + last + avg + pri != 100)
                    bad.Text = "הסכום של המאפיינים אינו 100";
                else //total is 100
                {
                    //updating criterea in database.
                    queries.UpdateCriterea(dist, total, last, avg, pri);

                    // Update the location based notifications
                    queries.SetIsLocationBasedNotificationsAllowed(chkAllowLocationBasedNotifications.Checked);
                    queries.SetLocationBasedNotificationsRadius(Int32.Parse(txtRadius.Text));

                    // Update the mail of the order center
                    queries.UpdateEmail(mailTxtbox.Text);

                    //redirecting the administrater back to his homepage,
                    Response.Redirect("~/Admin/AdminMenu.aspx");
                }
            }
            //if there was invalade inpute- notify user.
            else
            {
                bad.Text = "קלט לא תקין";
            }
        }
    }
}